package com.kl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kl.em.ConstantEnum;
import com.kl.em.MD5Utils;
import com.kl.em.Result;
import com.kl.entity.Role;
import com.kl.entity.Staff;
import com.kl.entity.StaffRole;
import com.kl.mapper.RoleMapper;
import com.kl.mapper.StaffMapper;
import com.kl.mapper.StaffRoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kl.service.IStaffService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private StaffRoleMapper staffRoleMapper;

    @Override
    @Transactional
    public Staff selectByName(String username) {

        QueryWrapper wrapper = new QueryWrapper<Staff>();
        wrapper.eq("name",username);
        Staff staff = staffMapper.selectOne(wrapper);
        return staff;
    }

    @Override
    @Transactional
    public Staff findStaffById(Long id) {
        Staff staff = staffMapper.selectById(id);
        return staff;
    }

    @Override
    @Transactional
    public List<Role> findRole() {
        List<Role> roles = roleMapper.selectList(null);
        return roles;
    }

    @Override
    @Transactional
    public List<Integer> findStaffRoleById(Long id) {
        List<Integer> list = staffRoleMapper.findStaffRoleById(id);
        return list;
    }

    @Override
    @Transactional
    public Result updateInfo(Staff staff, Integer[] rids) {
        int i = staffMapper.updateById(staff);
        if(i <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        // 先删除中间表信息 (uid)
        HashMap hashMap = new HashMap();
        hashMap.put("sid",staff.getId());
        int i1 = staffMapper.deleteByMap(hashMap);
        if(i1 <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        // 批量增加
        int i2 = staffRoleMapper.insertBatch(staff.getId(), rids);
        if(i2 <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    @Override
    @Transactional
    public List<Staff> getInfo() {
        List<Staff> staffs = staffMapper.selectList(null);
        return staffs;
    }

    @Override
    public Staff selectName(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",username);
        Staff staff = staffMapper.selectOne(queryWrapper);
        return staff;
    }



    @Override
    @Transactional
    public Result deleteStaffById(Long id) {
        int i = staffMapper.deleteById(id);
        if(i <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        // 先删除中间表信息 (aid)
        QueryWrapper wrapper = new QueryWrapper<Staff>();
        wrapper.eq("sid",id);
        int delete = staffRoleMapper.delete(wrapper);
        if(delete <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

//    @Override
//    public Result insert(StaffDTO staff) {
//        int insert = staffMapper.insert(staff);
//        if (insert <= 0){
//            return Result.build(ConstantEnum.ERROR);
//        }
//        return Result.build(ConstantEnum.SUCCESS);
//    }

    @Override
    @Transactional
    public Result addInfo(Staff staff, Integer[] rids) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        // 初始化密码
        staff.setPassword("123456");
        String pass = MD5Utils.getEncryptedPwd("123456");
        staff.setPassword(pass);
        String id = RandomStringUtils.randomNumeric(10);
        staff.setId(Long.parseLong(id));
        // 增加用户表 (返回主键)
        int i = staffMapper.insert(staff);
        if (i <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        // 增加中间表(uid  rid)

        int i2 = staffRoleMapper.insertBatch(Long.parseLong(id), rids);
        if (i2 <= 0){
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    @Override
    public List<Long> selectBySiteIdList(List<Long> siteIdList) {
        List<Long> list = staffMapper.selectSiteIdList(siteIdList);
        return list;
    }
}
